from django.contrib.auth.models import AbstractUser
from django.db import models
from django.contrib.auth.hashers import make_password

class User(AbstractUser):
    USER_TYPE_CHOICES = (
        ('admin', '管理员'),
        ('teacher', '教师'),
        ('student', '学生'),
    )
    user_type = models.CharField(max_length=20, choices=USER_TYPE_CHOICES, verbose_name="用户类型")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    # 新增微信相关字段
    wechat_openid = models.CharField(max_length=128, unique=True, null=True, blank=True, verbose_name="微信OpenID")
    wechat_avatar = models.URLField(null=True, blank=True, verbose_name="微信头像URL")
    wechat_phone = models.CharField(max_length=20, null=True, blank=True, verbose_name="微信手机号")

    class Meta:
        verbose_name = "用户"
        verbose_name_plural = "用户"
        db_table = "user"

    def save(self, *args, **kwargs):
        if self.password and not self.password.startswith(('pbkdf2_sha256$', 'bcrypt$', 'argon2$')):
            self.password = make_password(self.password)
        super().save(*args, **kwargs)